﻿Public Class clsCoreLogic

    Private c_Library As clsLibrary
    
    Public Event DataChanged()
    Public Event ViewChanged()
   
    Private Shared _thisInstance As clsCoreLogic

    Private c_DisplayLines As String()
    Private c_numLines As Int32
    Private c_DisplayLineSelected As Int32

    Private c_visibleLines As List(Of String)

    Public Property Library() As clsLibrary
        Get
            Return c_Library
        End Get
        Set(ByVal value As clsLibrary)
            c_Library = value
        End Set
    End Property

    Public Property DisplayLines() As String()
        Get
            MsgBox("do stuff here to create display")
            Return c_DisplayLines
        End Get
        Set(ByVal value As String())

            c_DisplayLines = value
        End Set
    End Property

    Public ReadOnly Property DisplayLineSelected() As Int32
        Get
            Return c_DisplayLineSelected
        End Get
       
    End Property


    Private Sub New()


        c_numLines = Configuration.ConfigurationSettings.AppSettings("NumberLines")
        ReDim c_DisplayLines(c_numLines - 1)
        c_DisplayLineSelected = 1

    End Sub

    Public Shared Function GetSingleton() As clsCoreLogic

        ' initialize object if it hasn't already been done
        If _thisInstance Is Nothing Then
            _thisInstance = New clsCoreLogic
        End If

        ' return the initialized instance
        Return _thisInstance

    End Function


    Public Sub LoadFromXml(ByVal path As String, Optional ByVal strDefaultXML As String = "")
        'Create as serializer and get the file to deserialize
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(clsLibrary))
        Dim file As New System.IO.FileStream(path, IO.FileMode.Open)
        Dim reader As New System.Xml.XmlTextReader(file)

        'Deserialize the file and populate the treeview

        c_Library = CType(ser.Deserialize(reader), clsLibrary)

        'Tidy up
        reader.Close()
        file.Close()
        file = Nothing

        For i As Int32 = 1 To c_numLines
            MsgBox("next to do here !!!!!!")
            c_DisplayLines(i) = c_Library.VisibleLines(c_Library.SelectedItem)
        Next

        RaiseEvent DataChanged()

    End Sub

    Public Sub NewLibrary()

        Dim newTopGroup As New clsTopGroup

        With newTopGroup
            .Name = "New group"
        End With

        c_Library = New clsLibrary
        c_Library.Topgroups = New List(Of clsTopGroup)
        c_Library.Topgroups.Add(newTopGroup)

        RaiseEvent DataChanged()

    End Sub


    Public Sub SaveXml(ByVal path As String)
        'Create as serializer and file to save TreeViewData
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(clsLibrary))
        Dim file As New System.IO.FileStream(path, IO.FileMode.Create)
        Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

        ser.Serialize(writer, c_Library)

        'Tidy up
        writer.Close()
        file.Close()
        file = Nothing

        RaiseEvent DataChanged()
    End Sub

    Public Sub Right()

        'select the current item

        'are we on a group, if so expand it
        If c_Library.SelectedItem.GetType.Equals(GetType(clsTopGroup)) Then
            For Each objTopgroup In c_Library.Topgroups
                objTopgroup.IsExpanded = False
            Next
            c_Library.Topgroups(c_Library.SelectedTopGroup).IsExpanded = True
        End If
        If c_Library.SelectedItem.GetTypeType.Equals(GetType(clsSubGroup)) Then
            For Each objTopgroup In c_Library.Topgroups
                For Each objSubGroup In objTopgroup.Subgroups
                    objSubGroup.IsExpanded = False
                Next
                If objTopgroup.SelectedSubGroup > -1 Then
                    objTopgroup.Subgroups(objTopgroup.SelectedSubGroup).IsExpanded = True
                End If
            Next

        End If

        RaiseEvent ViewChanged()
    End Sub
    Public Sub Left()

        RaiseEvent ViewChanged()
    End Sub
    Public Sub Up()

        'select the previous item in the current group, or the previous group 

        RaiseEvent ViewChanged()
    End Sub
    Public Sub Down()

        If c_DisplayLineSelected < c_numLines Then
            c_DisplayLineSelected += 1
        Else
            'scroll down
        End If

        For i As Int32 = c_DisplayLineSelected To 1 Step -1
            c_DisplayLines(i) = c_Library.VisibleLines(c_Library.SelectedItem)
        Next

        RaiseEvent ViewChanged()
    End Sub

End Class
